using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate;
using NHibernate;

namespace Bamboo.Core.Data
{
    public class HibernateQueryAllCallback : IHibernateCallback
    {
        private long totalRecord;
        private int currentPage;
        private int pageSize;
        private string className;
        private string sortField;
        private SortDirection sortDirection;
        public HibernateQueryAllCallback(string className, string sortField, 
            SortDirection sortDirection, int pageSize, int currentPage)
        {
            this.className = className;
            this.sortField = sortField;
            this.sortDirection = sortDirection;
            this.currentPage = currentPage;
            this.pageSize = pageSize;
        }
        public SortDirection SortDirection
        {
            get { return sortDirection; }
            set { sortDirection = value; }
        }

        public string SortField
        {
            get { return sortField; }
            set { sortField = value; }
        }

        public string ClassName
        {
            get { return className; }
            set { className = value; }
        }


        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }

        public long TotalRecord
        {
            get { return totalRecord; }
            set { totalRecord = value; }
        }

        
        public object DoInHibernate(NHibernate.ISession session)
        {
            //count number of item
            string countHQL = "select count(*) from " + className + " item ";
            IQuery countQuery = session.CreateQuery(countHQL);
            object countResult = countQuery.UniqueResult();
            if (countResult != null)
            {
                totalRecord = long.Parse(countResult.ToString());
            }

            //return the result
            string hql = "select item from " + className + " item ";
            if (sortField != null)
            {
                hql += " order by item." + sortField + " ";
                switch (sortDirection)
                {
                    case SortDirection.ASCENDING:
                        hql += " asc ";
                        break;
                    case SortDirection.DESCENDING:
                        hql += " desc ";
                        break;
                    default:
                        break;
                }
            }
            IQuery query = session.CreateQuery(hql);
            if (pageSize != -1 && currentPage != -1)
            {
                query.SetFirstResult(currentPage * pageSize);
                query.SetMaxResults(pageSize);
            }

            return query.List();
        }
    }
}
